package com.hall.controller;


import com.hall.pojo.Appointment;
import com.hall.pojo.R;
import com.hall.pojo.SysUser;
import com.hall.pojo.common.Result;
import com.hall.service.MeetDetailsService;
import com.hall.service.TencentCosService;
import com.hall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/files")
public class TencentCosController {
    @Autowired
    private TencentCosService tencentCosService;
    //原项目接口
    @Autowired
    private UserService userService;
    @Autowired
    private MeetDetailsService meetDetailsService;
    @ResponseBody
    @PostMapping("/upload")
    public Result upload(@RequestParam("report_file")MultipartFile report_file, String appointment_id, String report_id, String report_time, String clientfilename,String user_id,String clientfiletype, String filepath, HttpServletRequest request){
        return tencentCosService.upload(report_file,appointment_id,user_id,report_id,report_time,clientfilename,clientfiletype,filepath,request);
    }
    //App使用的接口
    @ResponseBody
    @PostMapping("/uploadFromApp")
    public Result uploadFromApp(@RequestParam("file")MultipartFile file, String appointmentId,String userId,String passWord,HttpServletRequest request){
        String report_id="1";
        String report_time="08:00:00";
        if(appointmentId==null || userId==null || passWord==null)return Result.error("存在未填写的参数");
        String clientfilename = file.getOriginalFilename();
        if(clientfilename==null || clientfilename.equals("")){
            return Result.error("文件为空,上传失败");
        }
        if(meetDetailsService.selectAppById(appointmentId).getData()==null)return Result.error("不存在的会议id");
        SysUser sys2 = userService.getUser(userId);
        if(sys2==null)return Result.error("不存在的用户");
        if(!passWord.equals(sys2.getPassword()))return Result.error("用户密码不匹配");
        String clientfiletype = clientfilename.substring(clientfilename.lastIndexOf("."));//获取图片文件名后缀
        String filepath="";
        return tencentCosService.upload(file,appointmentId,userId,report_id,report_time,clientfilename,clientfiletype,filepath,request);
    }
    @ResponseBody
    @PostMapping("/takeFile")
    public Result<String> takeFile(String filePath,String fileId){
       System.out.println(">>>>>>>>>>>>>>>>>"+filePath+fileId);
       String  data = tencentCosService.previewFile(filePath);
       return Result.success(data,"请求成功");
    }
    @ResponseBody
    @GetMapping("/download/{id}")
    public Result download(@PathVariable String id){
        return tencentCosService.download(id);
    }
}
